package com.hgyc.mom.qms.service.impl;

import com.hgyc.mom.mes.entity.MesWorkshop;
import com.hgyc.mom.mes.service.MesWorkshopService;
import com.hgyc.mom.qms.vo.InspectionTerminalVO;
import com.hgyc.mom.qms.vo.TerminalItemVO;
import com.hgyc.mom.system.entity.SysPost;
import com.hgyc.mom.system.service.SysPostService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.qms.mapper.QmsInspectionTerminalMapper;
import com.hgyc.mom.qms.entity.QmsInspectionTerminal;
import com.hgyc.mom.qms.entity.QmsInspectionTerminalItem;
import com.hgyc.mom.qms.service.QmsInspectionTerminalItemService;
import com.hgyc.mom.qms.vo.QmsInspectionTerminalVO;
import com.hgyc.mom.qms.service.QmsInspectionTerminalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 质量检测终端 Service业务层实现
 *
 * @author fwj
 * @since 2025-07-02
 */
@Slf4j
@Service
public class QmsInspectionTerminalServiceImpl extends ServiceImpl<QmsInspectionTerminalMapper, QmsInspectionTerminal> implements QmsInspectionTerminalService {

    @Autowired
    private QmsInspectionTerminalItemService qmsInspectionTerminalItemService;

    @Autowired
    private SysPostService sysPostService;

    @Autowired
    private MesWorkshopService mesWorkshopService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTerminal(QmsInspectionTerminalVO qmsInspectionTerminalVO) {
        // TODO 其他逻辑
        save(qmsInspectionTerminalVO);

        //新增检测终端不良采集项目
        List<QmsInspectionTerminalItem> items = qmsInspectionTerminalVO.getAddItems();
        if (!CollectionUtils.isEmpty(items)) {
            items.forEach(item -> {
                item.setId(null);
                item.setTerminalId(qmsInspectionTerminalVO.getId());
            });
            qmsInspectionTerminalItemService.saveBatch(items);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTerminal(QmsInspectionTerminalVO qmsInspectionTerminalVO) {
        // TODO 其他逻辑
        updateById(qmsInspectionTerminalVO);

        //删除检测终端不良采集项目
        if (!CollectionUtils.isEmpty(qmsInspectionTerminalVO.getDeleteItemIds())) {
            qmsInspectionTerminalItemService.removeBatchByIds(qmsInspectionTerminalVO.getDeleteItemIds());
        }

        //新增检测终端不良采集项目
        List<QmsInspectionTerminalItem> items = qmsInspectionTerminalVO.getAddItems();
        if (!CollectionUtils.isEmpty(items)) {
            items.forEach(item -> {
                item.setId(null);
                item.setTerminalId(qmsInspectionTerminalVO.getId());
            });
            qmsInspectionTerminalItemService.saveBatch(items);
        }

        //更新检测终端不良采集项目
        if (!CollectionUtils.isEmpty(qmsInspectionTerminalVO.getUpdateItems())) {
            List<QmsInspectionTerminalItem> updateItems = qmsInspectionTerminalVO.getUpdateItems();
            qmsInspectionTerminalItemService.updateBatchById(updateItems);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTerminal(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除检测终端不良采集项目
        qmsInspectionTerminalItemService.deleteBatchByParentIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<QmsInspectionTerminal> qmsInspectionTerminalList, boolean updateSupport) {
        if (qmsInspectionTerminalList == null || qmsInspectionTerminalList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + qmsInspectionTerminalList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + qmsInspectionTerminalList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("质量检测终端 导入失败", e);
            throw new BizException("质量检测终端 导入失败：" + e.getMessage());
        }
    }

    @Override
    public InspectionTerminalVO getInfoByIp(String ip) {
        LambdaQueryWrapper<QmsInspectionTerminal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QmsInspectionTerminal::getIp, ip);
        QmsInspectionTerminal inspectionTerminal = getOne(queryWrapper, false);
        if (inspectionTerminal == null) {
           throw new BizException("该终端没有维护设备信息，请联系管理员！");
        }
        InspectionTerminalVO inspectionTerminalVO = new InspectionTerminalVO();

        //产线信息
        if (inspectionTerminal.getLineId() != null) {
            MesWorkshop line = mesWorkshopService.getById(inspectionTerminal.getLineId());
            inspectionTerminalVO.setLineName(line.getName());
        }

        //岗位信息
        if (inspectionTerminal.getPostId() != null) {
            SysPost post = sysPostService.getById(inspectionTerminal.getPostId());
            inspectionTerminalVO.setPostName(post.getPostName());
        }

        BeanUtils.copyProperties(inspectionTerminal, inspectionTerminalVO);
        List<TerminalItemVO> itemVOList = qmsInspectionTerminalItemService.getByTerminalId(inspectionTerminalVO.getId());
        inspectionTerminalVO.setDetailList(itemVOList);

        return inspectionTerminalVO;
    }
}
